package com.zyasdk.service;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.zyasdk.model.ARModel;
import com.zyasdk.util.JSONHelper;
import com.zyasdk.util.StaticSetting;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.util.List;

public class GetServerValue {
	
	Context ct;
	
	public GetServerValue(Context context) {
        ct = context;
	}
	
	public void connet(String url, List<NameValuePair> params,int tryCount, Handler handler, Object object){
		new connetServer(ct,url,params,tryCount,handler,object).start();
	}
	
	class connetServer extends Thread {
		Context ct = null;
		String url = null;
		List<NameValuePair> params = null;
		int tryCount = 3;
		Handler handler = null;
		Object object = null;
		connetServer(Context context,String url, List<NameValuePair> params,int tryCount, Handler handler, Object object){
			this.ct = context;
			this.url = url;
			this.params = params;
			this.tryCount = tryCount;
			this.handler = handler;
			this.object = object;
		}
		@Override  
        public void run(){
			if(tryCount == 0) tryCount = 1;
			if(null == url ) return;
			String response = null;
			for(int i=0;i<tryCount;i++) {
//				Log.i("GetNewVersions11", "run "+url+"?"+params);
				response = GetResponse(url ,params);
				Log.i("result", "run + GetServerValue "+response);
				if(null == response) {
					try{
						Thread.sleep(10 * 60 * 1000);
					}catch (Exception e){
						e.printStackTrace();
					}
					continue;
				}else{
					break;
				}
			}
			
			if (handler != null && response != null) {
				try{
					JSONObject object = new JSONObject(response);
					ARModel model = getARModel(object);
					Bundle bundle = new Bundle();
					bundle.putSerializable("result", model);
					Message message = new Message();
					message.what = StaticSetting.RESULT_OK;
					message.obj = object;
					message.setData(bundle);
					handler.sendMessage(message);
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		}
		
		String GetResponse(String url, List<NameValuePair> params) {
			HttpPost request = new HttpPost(url);
			try {
				if (params != null) {
					request.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
				}
				request.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
				HttpResponse response = new DefaultHttpClient().execute(request);
				if (response.getStatusLine().getStatusCode() == 200) {
					return EntityUtils.toString(response.getEntity());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		ARModel getARModel(JSONObject object) {
			ARModel model = null;
			try {
				if (object != null) {
					model = JSONHelper.parseObject(object, ARModel.class);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return model;
		}
	}
}
